<?php

namespace App\Models\Admin;

use App\Models\BaseModel;
use App\Models\Admin\Store;
use App\Models\Admin\Good;
use Auth;
use App\Common\ValidateHandler;

class GoodType extends BaseModel
{
    protected $fillable = [
        'id', 'store_id', 'title', 'mark',
        'sort', 'status', 'created_at', 'updated_at'
    ];

    public function getList($data)
    {
        $pageSize = $data['pageSize'];
        $storeId = Auth::guard('admin')->user()->store_id;
        $requestStoreId = $data['storeId'];

        if ($storeId == 0) {
            // 管理员查看全部店铺分类列表 按店铺排序
            if ($requestStoreId == 0) {
                $data = $this->orderBy('store_id')->orderBy('sort', 'desc')->paginate($pageSize)->toArray();
            } else {
                $data = $this->where('store_id', $requestStoreId)->orderBy('sort', 'desc')->paginate($pageSize)->toArray();
            }
        } else {
            // 店家只能查看自己店的分类
            $data = $this->where('store_id', $storeId)->orderBy('sort', 'desc')->paginate($pageSize)->toArray();
        }
        $data = $this->handleData($data);

        return $this->success($data);
    }

    private function handleData($data)
    {
        $store = new Store;
        $stores = $store->select(['id', 'title'])->get()->toArray();
        $stores = key_val($stores);
        $stores[0] = ['id' => 0, 'title' => '无店铺'];

        foreach ($data['data'] as $k => $goodType) {
            $data['data'][$k]['store'] = $stores[$goodType['store_id']];
        }

        return $data;
    }

    public function getStore()
    {
        $store = new Store;
        $data = $store->select(['id', 'title'])->get()->toArray();

        return $this->success($data);
    }

    public function handle($request)
    {
        $id = $request['id'];

        $arr = [];
        $arr['title'] = $request['title'];
        $arr['mark'] = empty($request['mark']) ? '' : $request['mark'];
        $arr['status'] = $request['status'];
        $arr['sort'] = $request['sort'];

        $userStoreId = Auth::guard('admin')->user()->store_id; //用户店铺id 0为管理员
        if ($userStoreId == 0) {
            // 管理员提交
            $arr['store_id'] = empty($request['store_id']) ? 0 : $request['store_id'];
            if ($arr['store_id'] == 0) return $this->error(['info' => '请选择店铺']);
        } else {
            $arr['store_id'] = $userStoreId;
        }

        // 验证数据
        $result = $this->validate($arr);
        if (!$result['res']) {
            return $this->error($result);
        }

        if ($id == 'new') {
            $this->create($arr);
        } else {
            $this->where('id', $id)->update($arr);
        }

        return $this->success(['info' => '操作成功']);
    }

    private function validate($data){
        $arr = [
            0 => [
                'name' => '商品分类名称',
                'value' => $data['title'],
                'func' => 'is_empty'
            ]
        ];

        return ValidateHandler::do($arr);
    }

    public function deleteAction($id){
        $good = new Good;
        $good_data = $good->where("good_type_id",$id)->first();
        if(!empty($good_data)){
            return $this->error(["info"=>"该分类下存在商品，无法删除"]);
        }
        $this->where("id",$id)->delete();
        return $this->success(["info"=>"删除成功"]);   
    }
}
